<?php

namespace Tasker\Models;

class FilesModel extends BaseModel
{

	public function getProjectFiles(array $where=array())
	{
		return $this->connection->project_file()->where($where);
	}

	public function getProjectFile($where)
	{
		return $this->connection->project_file[$where];
	}

	public function editProjectFile($values)
	{
		$id = $values['id'];
		unset($values['id']);
		return $this->connection->project_file[$id]->update((array) $values);
	}

	public function deleteProjectFile($id)
	{
		$file = $this->getProjectFile(array('id' => $id));
		if (@unlink($file['path']))
		{
			return $file->delete();
		} else
		{
			return false;
		}
	}

	public function getTaskFiles(array $where=array())
	{
		return $this->connection->task_file()->where($where);
	}

	public function getTaskFile($where)
	{
		return $this->connection->task_file[$where];
	}

	public function editTaskFile($values)
	{
		$id = $values['id'];
		unset($values['id']);
		return $this->connection->task_file[$id]->update((array) $values);
	}

	public function deleteTaskFile($id)
	{
		$file = $this->getTaskFile(array('id' => $id));
		if (is_file($file['path']) && unlink($file['path']))
		{
			return $file->delete();
		} else
		{
			return false;
		}
	}

	public function getFileTypes()
	{
		return $this->connection->file_type();
	}

	public function getFileType(array $where)
	{
		return $this->connection->file_type[$where];
	}

	public function addFileType($name)
	{
		$affected = $this->connection->file_type()->insert(array('name' => $name));
		if ($affected === FALSE)
		{
			return false;
		}
		return true;
	}

	public function editFileType($values)
	{
		$affected = $this->connection->file_type[$values['id']]->update((array) $values);
		if ($affected === FALSE)
		{
			return false;
		}
		return true;
	}

	public function deleteFileType($id)
	{
		$affected = $this->connection->file_type[$id]->delete();
		if ($affected === FALSE)
		{
			return false;
		}
		return true;
	}

	public function addProjectFile($values)
	{
		$values = (array) $values;
		return $this->connection->project_file()->insert($values);
	}
	
	public function addTaskFile($values)
	{
		$values = (array) $values;
		return $this->connection->task_file()->insert($values);
	}

}
